import { throttle } from '@/utils/common';
import { useEffect, useState, Ref, RefObject } from 'react';

export function useWindowResize(callbackRef: RefObject<Fn>, delayMs = 100) {
  // 监听窗口大小变化
  useEffect(() => {
    const callback = () => {
      callbackRef?.current?.(document.documentElement.clientWidth);
    };
    const throCallback = throttle(callback, delayMs / 1000);
    window.addEventListener('resize', throCallback);

    return () => {
      window.removeEventListener('resize', throCallback);
    };
  }, [delayMs, callbackRef]);
}
